﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using ERPTOOLS.Class;

namespace ERPTOOLS
{
    public partial class DBCon : Form
    {
        bool boltest;
        public DBCon()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.TopMost = true;
        }
        private DataTable getTable(string strCon, string strSql, string strTable)
        {
            try
            {
                SqlConnection sqlcon = new SqlConnection(strCon);
                SqlDataAdapter da = new SqlDataAdapter(strSql, sqlcon);
                DataTable dt = new DataTable(strTable);
                da.Fill(dt);
                sqlcon.Close();
                return dt;
            }
            catch
            {
                return null;
            }
        }
        private void llbltest_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            comboBox2.Items.Clear();
            comboBox2.Text="";
            try
            {
                string strServer = textBox1.Text;
                string strUID = textBox2.Text;
                string strPwd = textBox3.Text;
                string str1 = "Data Source=" + strServer + ";database=master;Uid=" + strUID + ";Pwd=" + strPwd + ";";
                DataTable myTable = getTable(str1, "select name from sysdatabases", "sysdatabases");
                for (int i = 0; i < myTable.Rows.Count; i++)
                {
                    comboBox2.Items.Add(myTable.Rows[i]["name"].ToString());
                }
                comboBox2.Text = myTable.Rows[0]["name"].ToString();
                lblmessage.Visible = true;
                lblmessage.Text = "连接成功。";
                comboBox2.Text = "";
                boltest = true;
                
            }
            catch
            {
                lblmessage.Visible = true;
                lblmessage.Text="连接失败，请检查网络、用户名、密码！";
            }
        }

        private void btcancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btconfirm_Click(object sender, EventArgs e)
        {
            if (boltest == true)
            {
                if (comboBox2.Text != "")
                {
                    XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//DB", "DBIP", textBox1.Text);
                    XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//DB", "DBUser", textBox2.Text);
                    XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//DB", "DBPassword", textBox3.Text);
                    XMLHelper.CreateOrUpdateXmlNodeByXPath("Configure.xml", "//DB", "Database", comboBox2.Text);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("请选择数据库");
                }
            }
            else
            {
                MessageBox.Show("请先测试连接，并选择数据库");
            }
        }

        private void DBCon_Load(object sender, EventArgs e)
        {
            textBox1.Text = XMLHelper.GetInnerTextByXpath("Configure.xml", "//DBIP").ToString();
            textBox2.Text = XMLHelper.GetInnerTextByXpath("Configure.xml", "//DBUser").ToString();
            textBox3.Text = XMLHelper.GetInnerTextByXpath("Configure.xml", "//DBPassword").ToString();
            comboBox2.Text = XMLHelper.GetInnerTextByXpath("Configure.xml", "//Database").ToString();
        }
    }
}
